<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Storage;
use App\Models\AgentKf;
use Zhiyi\Plus\Support\Configuration;
use Illuminate\Support\Facades\Validator;

class AgsysController extends Controller
{
	//模板
    public $v = 'admin.agsys.';
	//分页
	public $paginate = 15;
	//路径	
	public $dirpath;
	
	public function __construct()
	{
		$this->dirpath = base_path('storage').'/app/system/';
		$this->saveConfig([], 'kefu');
		$this->saveConfig([], 'prom');
	}

	/**
	 * 提现客服设置
	 * @param Request $request
	 * @return type
	 */	
	public function kefu(Request $request){
		$kefu = require_once $this->dirpath.'kefu.php';
		if($request->isMethod('post')){
			$params = $request->all();
			
			$validator = Validator::make($params, [
				'contact' => 'regex:/^([\xe4-\xe9][\x80-\xbf]{2}){2,4}$/',
				'mobile' => 'regex:/^1[3-9]\d{9}$/',				
				'wechat' => 'regex:/^[a-zA-Z0-9_]+$/',					
				'email' => 'regex:/^[a-z0-9]+([._-][a-z0-9]+)*@([0-9a-z]+\.[a-z]{2,14}(\.[a-z]{2})?)$/i',
				'qq' => 'regex:/^[1-9]\d{4,10}$/',
			],[
				'contact.regex' => '联系人必须是中文'	,
				'mobile.regex' => '手机号不正确',
				'wechat.regex' => '微信号不正确',						
				'email.regex' => '邮箱不合法',
				'qq.regex' => 'QQ号不正确',
			]);
			$error = $validator->errors()->first();
			if ($error) {
				return ajaxError($error);
			}
			$content = [
					'contact' => $params['contact'],
					'wechat' => $params['wechat'],
					'wechat_img' => $params['wechat_img'],
					'mobile' => $params['mobile'],
					'email' => $params['email'],
					'qq' => $params['qq'],
					'updated_at' => date('Y-m-d H:i:s'),
			];			
			$this->saveConfig($content, 'kefu');
			return ajaxSuccess();
		}
		return view($this->v . 'kefu', compact('kefu'));
	}
	/**
	 * 推广设置
	 * @param Request $request
	 * @return type
	 */	
	public function prom(Request $request){
		$prom = require_once $this->dirpath.'prom.php';
		if($request->isMethod('post')){
			$params = $request->all();
			/*$validator = Validator::make($params, [
				'contact' => 'regex:/^([\xe4-\xe9][\x80-\xbf]{2}){2,4}$/',
				'mobile' => 'regex:/^1[3-9]\d{9}$/',				
				'wechat' => 'regex:/^[a-zA-Z0-9_]+$/',					
				'email' => 'regex:/^[a-z0-9]+([._-][a-z0-9]+)*@([0-9a-z]+\.[a-z]{2,14}(\.[a-z]{2})?)$/i',
				'qq' => 'regex:/^[1-9]\d{4,10}$/',
			],[
				'contact.regex' => '联系人必须是中文'	,
				'mobile.regex' => '手机号不正确',
				'wechat.regex' => '微信号不正确',						
				'email.regex' => '邮箱不合法',
				'qq.regex' => 'QQ号不正确',
			]);
			$error = $validator->errors()->first();
			if ($error) {
				return ajaxError($error);
			}*/
			$content = [
				'url' => $params['url'],
				'qcode_img' => $params['qcode_img'],					
				'updated_at' => date('Y-m-d H:i:s'),
			];			
			$this->saveConfig($content, 'prom');
			return ajaxSuccess();
		}
		return view($this->v . 'prom', compact('prom'));
	}
	/**
	 * 推广二维码
	 * @param Request $request
	 * @return type
	 */	
	public function qrcode(){
		echo 454;exit;
	}
	/**
	 * 上传客服二维图片
	 */
	public function kfupload(Request $request) {
		$file = request()->file('file');
		$type = $request->input('type');
		if($file==null){
			exit(json_encode(array('code'=>1,'msg'=>'未上传图片')));
		}
		// 获取文件后缀
		$temp = explode(".", $_FILES["file"]["name"]);
		$extension = end($temp);
		// 判断文件是否合法
		if(!in_array($extension,array("gif","jpeg","jpg","png"))){
			exit(json_encode(array('code'=>1,'msg'=>'上传图片不合法')));
		}
		//检测目录是否存在
		$dirpath = 'storage/'.$type.'/';
		if (!is_dir($dirpath)){
			mkdir(iconv("UTF-8", "GBK", $dirpath), 0777, true);
		}
		// 文件是否上传成功
	   if ($file->isValid()) {		  
		   // 获取文件相关信息			
			$newName = rand(100, 999).date('i').date('s').".".$extension;	
			$path = $file->move($dirpath, $newName);
			$namePath = '/'.$dirpath . $newName;
			exit(json_encode(array('code'=>0, 'msg'=> $namePath)));
	   }
	   exit(json_encode(array('code'=>1,'msg'=>'上传失败')));
	}
	/**
	 * 创建客服配置文件
	 * @param type $filename
	 * @param type $content
	 */
	
	public function saveConfig($content = [], $type=null)
	{
		if($type == 'kefu') {
			$filename = $this->dirpath.'kefu.php';
			if(!file_exists($filename) && empty($content)) {
				if (!is_dir($this->dirpath)){
					mkdir(iconv("UTF-8", "GBK", $this->dirpath), 0777, true);
				}
				file_put_contents($filename, "<?php\n\nreturn " . var_export($content, true) . ';');
			}		
			if(file_exists($filename) && !empty($content)) {
				file_put_contents($filename, "<?php\n\nreturn " . var_export($content, true) . ';');
			}
		}
		if($type == 'prom') {
			$promfilename = $this->dirpath.'prom.php';
			if(!file_exists($promfilename) && empty($content)) {
				if (!is_dir($this->dirpath)){
					mkdir(iconv("UTF-8", "GBK", $this->dirpath), 0777, true);
				}
				file_put_contents($promfilename, "<?php\n\nreturn " . var_export($content, true) . ';');
			}		
			if(file_exists($promfilename) && !empty($content)) {
				file_put_contents($promfilename, "<?php\n\nreturn " . var_export($content, true) . ';');
			}
		}	
	}
}